package com.hentai.androidserver.service.impl;

import com.hentai.androidserver.domain.Phone;
import com.hentai.androidserver.mapper.PhoneMapper;
import com.hentai.androidserver.service.PhoneService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class PhoneServiceImpl implements PhoneService {

    @Resource
    PhoneMapper phoneMapper;

    @Override
    public List<Phone> queryByUserId(Integer uid) {
        if (uid == null) {
            return null;
        }
        return phoneMapper.queryByUserId(uid);
    }

    @Override
    public List<Phone> makeChangeFromMobile(List<Phone> phones) {

        List<Phone> phoneList = new ArrayList<>();
//        phoneMapper.
        for (Phone phone : phones) {

            if (phone.getIsdelete() > 0) {
                // 删除
                phoneMapper.deleteByPid(phone.getPid());
            } else if (phone.getPid() > 0) {
                // 修改
                phoneMapper.updatePhoneByPhone(phone);
            } else {
                // 新增
                phoneMapper.addPhone(phone);
                phoneList.add(phone);
            }
        }

        return phoneList;
    }
}
